package com.pzj.dormitory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pzj.dormitory.entity.Dormitory;
import com.pzj.dormitory.entity.Moveout;
import com.pzj.dormitory.entity.Student;
import com.pzj.dormitory.form.SearchForm;
import com.pzj.dormitory.mapper.DormitoryMapper;
import com.pzj.dormitory.mapper.MoveoutMapper;
import com.pzj.dormitory.mapper.StudentMapper;
import com.pzj.dormitory.service.MoveoutService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pzj.dormitory.util.CommonUtil;
import com.pzj.dormitory.vo.MoveoutVO;
import com.pzj.dormitory.vo.PageVO;
import com.pzj.dormitory.vo.StudentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-04-28
 */
@Service
public class MoveoutServiceImpl extends ServiceImpl<MoveoutMapper, Moveout> implements MoveoutService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private DormitoryMapper dormitoryMapper;
    @Autowired
    private MoveoutMapper moveoutMapper;

    @Override
    public PageVO list(Integer pageNum, Integer pageSize) {
        Page<Student> studentPage = new Page<>(pageNum, pageSize);
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.eq("state","入住");
        Page<Student> page = studentMapper.selectPage(studentPage, studentQueryWrapper);
        List<Student> students = page.getRecords();
        ArrayList<StudentVO> studentVOS = new ArrayList<>();

        // VO转换
        for (Student student : students){
            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(student, studentVO);
            Dormitory dormitory = dormitoryMapper.selectById(student.getDormitoryId());
            studentVO.setDormitoryName(dormitory.getName());
            studentVOS.add(studentVO);
        }
        PageVO pageVO = new PageVO();
        pageVO.setTotal(page.getTotal());
        pageVO.setData(studentVOS);
        return pageVO;
    }

    @Override
    public PageVO search(SearchForm searchForm) {
        Page<Student> studentPage = new Page<>(searchForm.getPage(),searchForm.getSize());
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.eq("state","入住");
        Page<Student> page = null;
        if(searchForm.getValue().equals("")){
            page = studentMapper
                    .selectPage(studentPage, studentQueryWrapper);
        }else {
            studentQueryWrapper.like(searchForm.getKey(),searchForm.getValue());
            page = studentMapper.selectPage(studentPage, studentQueryWrapper);
        }
        List<Student> students = page.getRecords();
        ArrayList<StudentVO> studentVOS = new ArrayList<>();
        for (Student student : students){
            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(student,studentVO);
            Dormitory dormitory = dormitoryMapper.selectById(student.getDormitoryId());
            studentVO.setDormitoryName(dormitory.getName());
            studentVOS.add(studentVO);
        }
        PageVO pageVO = new PageVO();
        pageVO.setTotal(page.getTotal());
        pageVO.setData(studentVOS);
        return pageVO;
    }

    @Override
    public boolean moveout(Integer id, String reason) {
        Moveout moveout = new Moveout();
        Student student = studentMapper.selectById(id);
        moveout.setStudentId(String.valueOf(id));
        moveout.setDormitoryId(String.valueOf(student.getDormitoryId()));
        moveout.setReason(reason);
        moveout.setCreateDate(CommonUtil.createDate());
        int insert = moveoutMapper.insert(moveout);
        if(insert!=1)return false;
        student.setState("迁出");
        int i = studentMapper.updateById(student);
        if(i!=1)return false;
        // 迁出之后宿舍床位空闲加1
        try {
            dormitoryMapper.addAvailableById(student.getDormitoryId());
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public PageVO moveoutList(Integer pageNum, Integer pageSize) {
        Page<Moveout> page = new Page<>(pageNum, pageSize);
        Page<Moveout> moveoutPage = moveoutMapper.selectPage(page, null);
        List<Moveout> moveoutList = moveoutPage.getRecords();
        ArrayList<MoveoutVO> moveoutVOS = new ArrayList<>();

        for (Moveout moveout : moveoutList) {
            MoveoutVO moveoutVO = new MoveoutVO();
            BeanUtils.copyProperties(moveout, moveoutVO);
            // 查询学生信息，获取学生姓名
            Student student = studentMapper.selectById(moveout.getStudentId());
            System.out.println("student = " + student);
            // 查询宿舍信息，获取宿舍名称
            Dormitory dormitory = dormitoryMapper.selectById(moveout.getDormitoryId());
            moveoutVO.setStudentName(student.getName());
            moveoutVO.setDormitoryName(dormitory.getName());
            moveoutVOS.add(moveoutVO);
        }
        PageVO pageVO = new PageVO();
        pageVO.setData(moveoutVOS);
        pageVO.setTotal(moveoutPage.getTotal());

        return pageVO;
    }

    @Override
    public PageVO moveoutSearch(SearchForm searchForm) {
        Page<Moveout> moveoutPage = new Page<>(searchForm.getPage(), searchForm.getSize());
        Page <Moveout> page = null;
        if(searchForm.getValue().equals("")){
            page = moveoutMapper.selectPage(moveoutPage, null);
        }else {
            QueryWrapper<Moveout> moveoutQueryWrapper = new QueryWrapper<>();
            if(searchForm.getKey().equals("studentName")){
                QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
                studentQueryWrapper.like("name", searchForm.getValue());
                List<Student> students = studentMapper.selectList(studentQueryWrapper);
                List<Integer> idList = new ArrayList();
                for (Student student : students){
                    idList.add(student.getId());
                }
                moveoutQueryWrapper.in("student_id", idList);
            }
            if(searchForm.getKey().equals("dormitoryName")) {
                QueryWrapper<Dormitory> dormitoryQueryWrapper = new QueryWrapper<>();
                dormitoryQueryWrapper.like("name", searchForm.getValue());
                List<Dormitory> dormitories = dormitoryMapper.selectList(dormitoryQueryWrapper);
                List<Integer> idList = new ArrayList();
                for (Dormitory dormitory : dormitories){
                    idList.add(dormitory.getId());
                }
                moveoutQueryWrapper.in("dormitory_id", idList);
            }
            page = moveoutMapper.selectPage(moveoutPage, moveoutQueryWrapper);

        }
        List<Moveout> moveoutList = moveoutPage.getRecords();
        ArrayList<MoveoutVO> moveoutVOS = new ArrayList<>();

        for (Moveout moveout : moveoutList) {
            MoveoutVO moveoutVO = new MoveoutVO();
            BeanUtils.copyProperties(moveout, moveoutVO);
            // 查询学生信息，获取学生姓名
            Student student = studentMapper.selectById(moveout.getStudentId());
            System.out.println("student = " + student);
            // 查询宿舍信息，获取宿舍名称
            Dormitory dormitory = dormitoryMapper.selectById(moveout.getDormitoryId());
            moveoutVO.setStudentName(student.getName());
            moveoutVO.setDormitoryName(dormitory.getName());
            moveoutVOS.add(moveoutVO);
        }
        PageVO pageVO = new PageVO();
        pageVO.setData(moveoutVOS);
        pageVO.setTotal(moveoutPage.getTotal());

        return pageVO;
    }
}
